/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.*;
import pojo.*;
import utils.HibernateUtil;

/**
 *
 * @author DANG
 */
public class LichChieuPhimDAO {

    public static SessionFactory _localSessionFactory;
    public static Session _localSession;
    private final static String ERR_INVALID_PARAMS = "Invalid parameters !";

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static void openCurrentSession() throws Exception {
        try {
            if (_localSession != null) {
                _localSession.close();
            }
            _localSession = _localSessionFactory.openSession();
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "openCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        }
    }

    public static void closeCurrentSession() throws Exception {
        if (_localSession == null) {
            return;
        }
        try {
            _localSession.close();
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "closeCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            _localSession = null;
        }
    }

    public static List<Lichchieuphim> getLichChieuPhimsByRoomAndFilmAndDateAndShowtime(Phongchieu paramRoom, Phim paramFilm, 
            Date paramDate, Suatchieu paramShowtime) throws Exception {
        if (paramRoom == null || paramFilm == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        List<Lichchieuphim> items = null;
        Phongchieu tempRoom = paramRoom;
        Phim tempFilm = paramFilm;
        Date tempDate = paramDate;
        Suatchieu tempShowtime = paramShowtime;
//        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim AND l.phongchieu = :room AND l.phim = :film AND l.ngayChieu = :date AND l.suatchieu = :showtime";
        try {
            //Step 01: creat retrieving session using where clause.
            if (_localSession == null) {
                openCurrentSession();
            }
//            localSession = _localSessionFactory.openSession();
            _localSession.beginTransaction();
            //Step 02: begin retrieving transaction in above session.
//            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setEntity("showtime", tempShowtime);
            q.setDate("date", tempDate);
            q.setEntity("film", tempFilm);
            q.setEntity("room", tempRoom);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05: close retrieving session.
//                localSession.close();
//
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Lichchieuphim> getLichChieuPhimsByRoomAndFilmAndDate(Phongchieu paramRoom, Phim paramFilm, Date paramDate) throws Exception {
        if (paramRoom == null || paramFilm == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        List<Lichchieuphim> items = null;
        Phongchieu tempRoom = paramRoom;
        Phim tempFilm = paramFilm;
        Date tempDate = paramDate;
//        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim AND l.phongchieu = :room AND l.phim = :film AND l.ngayChieu = :date";
        try {
            //Step 01: creat retrieving session using where clause.
            if (_localSession == null) {
                openCurrentSession();
            }
//            localSession = _localSessionFactory.openSession();
            _localSession.beginTransaction();
            //Step 02: begin retrieving transaction in above session.
//            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setDate("date", tempDate);
            q.setEntity("film", tempFilm);
            q.setEntity("room", tempRoom);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05: close retrieving session.
//                localSession.close();
//
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Lichchieuphim> getLichChieuPhimsByRoomAndFilm(Phongchieu paramRoom, Phim paramFilm) throws Exception {
        if (paramRoom == null || paramFilm == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        List<Lichchieuphim> items = null;
        Phongchieu tempRoom = paramRoom;
        Phim tempFilm = paramFilm;
//        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim AND l.phongchieu = :room AND l.phim = :film";
        try {
            //Step 01: creat retrieving session using where clause.
            if (_localSession == null) {
                openCurrentSession();
            }
//            localSession = _localSessionFactory.openSession();
            _localSession.beginTransaction();
            //Step 02: begin retrieving transaction in above session.
//            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setEntity("film", tempFilm);
            q.setEntity("room", tempRoom);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05: close retrieving session.
//                localSession.close();
//
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Lichchieuphim> getLichChieuPhimsByRoom(Phongchieu paramRoom) throws Exception {
        if (paramRoom == null) {
            throw new Exception(ERR_INVALID_PARAMS);
        }
        List<Lichchieuphim> items = null;
        Phongchieu tempRoom = paramRoom;
//        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim AND l.phongchieu = :room";
        try {
            //Step 01: creat retrieving session using where clause.
            if (_localSession == null) {
                openCurrentSession();
            }
//            localSession = _localSessionFactory.openSession();
            _localSession.beginTransaction();
            //Step 02: begin retrieving transaction in above session.
//            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setEntity("room", tempRoom);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05: close retrieving session.
//                localSession.close();
//
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Lichchieuphim> getLichChieuPhims() throws Exception {
        List<Lichchieuphim> items = null;
//        Session localSession = null;
        String localQuery = "FROM Lichchieuphim l WHERE l.trangThai = :statusLichChieuPhim";
        try {
            //Step 01: creat retrieving session using where clause.
            if (_localSession == null) {
                openCurrentSession();
            }
//            localSession = _localSessionFactory.openSession();
            _localSession.beginTransaction();
            //Step 02: begin retrieving transaction in above session.
//            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setInteger("statusLichChieuPhim", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05: close retrieving session.
//                localSession.close();
//
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static boolean insertLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramLichChieuPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            localSession.save(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "insertLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "insertLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updateLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramLichChieuPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            localSession.update(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "updateLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "updateLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteLichChieuPhims(Lichchieuphim paramLichChieuPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramLichChieuPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            localSession.delete(paramLichChieuPhims);
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "deleteLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "deleteLichChieuPhims", LichChieuPhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
